import { Injectable } from '@angular/core';
import { Actions, createEffect, ofType } from '@ngrx/effects';
import { EMPTY } from 'rxjs';
import { map, exhaustMap, catchError } from 'rxjs/operators';
import { Service } from '../../service/service';
import { getList, getListSuccess} from "../actions/list.actions";

@Injectable()
export class ListEffects {
  loadList$ = createEffect(() => this.actions$.pipe(
    ofType(getList),
    exhaustMap(() => this.service.getAll()
      .pipe(
        map(data => (getListSuccess(data))),
        catchError(() => EMPTY)
      ))
    )
  );

  constructor(
    private actions$: Actions,
    private service: Service
  ) {}
}
